<?php

/**
*  存储静态页面类
*/

class Html{
	
	function Html($db){
		global $tpl;
		$this->data		= 'data/';
		$this->Tpl		= $tpl;
		$this->oDB		= $db;
	}
	
	
	/**
	*  写入静态文件
	*  name		文件名
	*  data		数据
	*/
	function writer(
			$wd = array(
				'write_id'=>'',				//写入文件名{不要后缀，为了适应多语言}
				'write_file'=>'',			//写入地址
				'subject'=>'',				//文章标题
				'data'=>'',//区块数据
		)
	){
		global $tpl_set,$sitename,$siteurl;
		
		$formart = 'html';
		
	 	$query = $this->oDB->query("SELECT * FROM _site_menu WHERE runmode=0 ORDER BY orders DESC,id DESC");
	 	$mi = 0;
	 	while($mls = $this->oDB->fetch_array($query)){
	 		$mi++;
	 		if($mls[names]=='Home'){
	 			$bar_txt  = $mls[desc];
	 		}
	 		
	 		//修正连接地址
	 		if (!ereg('http://',$mls['links'])){
	 			$mls['links'] = $siteurl.$mls['links'];
	 		}
	 		
	 		$mls['split'] = ($mi>1)?1:0;
	 		$page_menu[]  = $mls;
	 	}
	 	
		
		//设置页面信息
		$this->Tpl->set_var(
			array(
				"page_menu" => $page_menu,
				"_siteurl" 	=> $siteurl,
				"_title" 	=> $wd['subject'] . ' - ' . $sitename.'',
				"_page_info"=> $wd['data'],		//页面数据
				"_page_dir"=> $this->data.$wd['write_file'],		//写入的路径
				"_page_id"=> $wd['write_id'],	//写入文件名
			)
		);
		
		$this->Tpl->set_file('page',$tpl_set['TemplateDir'].'/global');
		$data = $this->Tpl->r();
		
		//替换站内连接
		$data = str_replace("{[siteurl]}",$siteurl,$data);
		
		//处理多余字符
		$data = str_replace("\t","",$data);
		
		//头部连接
		$cn_link	= '<a href="'.$wd['write_id'].'.'.$formart.'"><font color="#ffffff">中文简体</b></font></a>
<a href="'.$wd['write_id'].'_hk.'.$formart.'"><font color="#ffffff">香港繁体</font></a>
<a href="'.$wd['write_id'].'_tw.'.$formart.'"><font color="#ffffff">台灣繁体</font></a>';
		//<a href="#"><font color="#ffffff">English</font></a>
//<a href="#"><font color="#ffffff">한국어</font></a>
		
		if (trim($wd['write_id'])!=''){
			//字符集处理
			//香港繁体
			$data_hk  = Chinese($data,'big5','hk');
			$siteurls = str_replace("/","\\/",$siteurl);
			$siteurls = str_replace(".","\\.",$siteurls);
			$data_hk  = preg_replace("/".$siteurls."(.+?)\.".$formart."/",$siteurl."\\1_hk.".$formart,$data_hk);
			//头部连接
			$cn_links = str_replace("香港繁体","<b>香港繁体</b>",$cn_link);
			$data_hk  = str_replace("[page_title_language]",$cn_links,$data_hk);
			
			$this->Tpl->writer($this->data.$wd['write_file'].'/'.$wd['write_id'].'_hk.'.$formart,$data_hk);
			
			//台湾繁体
			$data_tw  = Chinese($data,'big5');
			$siteurls = str_replace("/","\\/",$siteurl);
			$siteurls = str_replace(".","\\.",$siteurls);
			$data_tw  = preg_replace("/".$siteurls."(.+?)\.".$formart."/",$siteurl."\\1_tw.".$formart,$data_tw);
			//头部连接
			$cn_links = str_replace("台灣繁体","<b>台灣繁体</b>",$cn_link);
			$data_tw  = str_replace("[page_title_language]",$cn_links,$data_tw);
			$this->Tpl->writer($this->data.$wd['write_file'].'/'.$wd['write_id'].'_tw.'.$formart,$data_tw);
			
			
			//简体中文
			$cn_links = str_replace("中文简体","<b>中文简体</b>",$cn_link);
			$data  	  = str_replace("[page_title_language]",$cn_links,$data);
			$this->Tpl->writer($this->data.$wd['write_file'].'/'.$wd['write_id'].'.'.$formart,$data.'<!-- 更新时间:'.date("Y年m月d日 H点i分").' -->');
		}
	}
	
}



?>